package NBJL;

import java.sql.ResultSet;
import java.text.DecimalFormat;
import javagprssrv.Common.DateTime;
import javagprssrv.DataBase.DBSQLExecute;
import javagprssrv.JGS_Param;
import javagprssrv.StructClass.structTask;
import javagprssrv.StructClass.structRecvData;

public class TrdTCPDataSaver_NBJL extends java.lang.Thread
{
    javagprssrv.StructClass.structRecvData recvData;
    javagprssrv.StructClass.structTask task;
    int JobIndex;
    java.lang.String _Protocol;
    int _UDPServerSocketIndex;
    java.lang.String _RemoteIP;
    int _RemotePort;
    java.lang.String _Data;
    java.lang.String _OriginRecvData;
    int RecvDataType;
    java.lang.String TaskID;
    int DataType;
    java.lang.String TermNo;
    java.lang.String YBH;
    java.lang.String AsynTelNo;
    java.lang.String DYDJ;
    java.lang.String zyxs;
    java.lang.String csxs;
    java.lang.String cxxs;
    java.lang.String zdzs;
    java.lang.String zxzs;
    java.lang.String hgs;
    double zdz;
    double zxz;
    double pjz;
    double z00;
    double z01;
    double z02;
    double z03;
    double z04;
    double z05;
    double z06;
    double z07;
    double z08;
    double z09;
    double z10;
    double z11;
    double z12;
    double z13;
    double z14;
    double z15;
    double z16;
    double z17;
    double z18;
    double z19;
    double z20;
    double z21;
    double z22;
    double z23;
    java.lang.String sYB_BL;
    java.lang.String szdz;
    java.lang.String szxz;
    java.lang.String spjz;
    java.lang.String sz00;
    java.lang.String sz01;
    java.lang.String sz02;
    java.lang.String sz03;
    java.lang.String sz04;
    java.lang.String sz05;
    java.lang.String sz06;
    java.lang.String sz07;
    java.lang.String sz08;
    java.lang.String sz09;
    java.lang.String sz10;
    java.lang.String sz11;
    java.lang.String sz12;
    java.lang.String sz13;
    java.lang.String sz14;
    java.lang.String sz15;
    java.lang.String sz16;
    java.lang.String sz17;
    java.lang.String sz18;
    java.lang.String sz19;
    java.lang.String sz20;
    java.lang.String sz21;
    java.lang.String sz22;
    java.lang.String sz23;
    java.lang.String YB_YBH;
    double YB_BL;
    int YB_BBXSW;
    java.lang.String YB_GSMC;
    java.lang.String YB_floatGS;
    java.lang.String s2_Parameter;
    java.lang.String s2_Year;
    java.lang.String s2_Month;
    java.lang.String s2_Date;
    
    public TrdTCPDataSaver_NBJL() {
        super("TrdTCPDataSaver_NBJL");
        this.JobIndex = -1;
        this.RecvDataType = -1;
        this.TermNo = "";
        this.YBH = "";
        this.AsynTelNo = "";
        this.DYDJ = "";
    }
    
    private boolean findRecvData() {
        boolean res = false;
        synchronized (javagprssrv.JGS_Param.UnSavedRecvData_TCP) {
            for (int i = 0; i < javagprssrv.JGS_Param.UnSavedRecvData_TCP.size(); ++i) {
                if (javagprssrv.JGS_Param.UnSavedRecvData_TCP.elementAt(i).RecvDataType == 6) {
                    this.recvData = javagprssrv.JGS_Param.UnSavedRecvData_TCP.remove(i);
                    res = true;
                    break;
                }
            }
            if (javagprssrv.JGS_Param.UnSavedRecvData_TCP.size() > 10000) {
                javagprssrv.JGS_Param.UnSavedRecvData_TCP.clear();
            }
        }
        this._Protocol = this.recvData.Protocol.trim();
        this._RemoteIP = this.recvData.RemoteIP;
        this._RemotePort = this.recvData.RemotePort;
        this._Data = this.recvData.Data;
        this._OriginRecvData = this.recvData.Data;
        return res;
    }
    
    @java.lang.Override
    public void run() {
        try
        {
         while(true)
          {
            Thread.sleep(20L);
            if (findRecvData())
            {
              this.DataType = this._Data.substring(5, 6).charAt(0);
              if (checkData())
              {
                if (this.DataType == 138) {
                  save_03();
                }
                if (this.DataType == 137) {
                  save_04();
                }
              }
            }
          }
        }
        catch (Exception e) {}
    }
    //数据库不符
    public void save_03() throws java.lang.Exception {
        this.ReadRecvDataGetValue();
        int MonthDataType;
        if (this.DataType == 243) {
            MonthDataType = 0;
        }
        else {
            MonthDataType = 1;
        }
        java.lang.String sql = "select count(*) from T_YSJ where 仪表号='" + this.YB_YBH + "' and 年月='20" + this.s2_Year + this.s2_Month + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
        rs.next();
        final int recordCounts = rs.getInt(1);
        java.lang.String _zyxs = "";
        final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
        if (recordCounts == 0) {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "insert into T_YSJ(年月,仪表号,保存日期,总运行时,超上限时,超下限时,最大值,最大值时,最小值,最小值时,平均值,种类,合格时,合格率,超上限率,超下限率) values (";
            sql = sql + "'20" + this.s2_Year + this.s2_Month + "','" + this.YB_YBH + "','" + time + "',";
            sql = sql + this.zyxs + "," + this.csxs + "," + this.cxxs + "," + this.szdz + ",'" + this.zdzs + "'," + this.szxz + ",'" + this.zxzs + "',";
            sql = sql + this.spjz + "," + java.lang.String.valueOf(MonthDataType) + "," + this.hgs + ",'" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.hgs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "'" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.csxs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "'" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.cxxs) / java.lang.Integer.valueOf(_zyxs)) + "')";
        }
        else {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "update T_YSJ set 年月='20" + this.s2_Year + this.s2_Month + "',仪表号='" + this.YB_YBH + "',保存日期='" + time + "',";
            sql = sql + "总运行时=" + this.zyxs + ",超上限时=" + this.csxs + ",超下限时=" + this.cxxs + ",最大值=" + this.szdz + ",最大值时='" + this.zdzs + "',最小值=" + this.szxz + ",最小值时='" + this.zxzs + "',平均值=" + this.spjz + ",";
            sql = sql + "超上限率='" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.csxs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "超下限率='" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.cxxs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "合格率='" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.hgs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "合格时=" + this.hgs + ",";
            sql = sql + "种类=" + java.lang.String.valueOf(MonthDataType) + " where 仪表号='" + this.YB_YBH + "' and 年月='20" + this.s2_Year + this.s2_Month + "'";
        }
        this.SQLExecute(sql);
    }
    //数据库不符
    public void save_04() throws java.lang.Exception {
        this.ReadRecvDataGetValue();
        java.lang.String sql = "select count(*) from T_RSJ where 仪表号='" + this.YB_YBH + "' and 日期='20" + this.s2_Year + this.s2_Month + this.s2_Date + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
        rs.next();
        final int recordCounts = rs.getInt(1);
        java.lang.String _zyxs = "";
        final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
        if (recordCounts == 0) {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "insert into T_RSJ(日期,仪表号,保存日期,总运行时,超上限时,超下限时,最大值,最大值时,最小值,最小值时,平均值,";
            sql += "h00,h01,h02,h03,h04,h05,h06,h07,h08,h09,h10,h11,h12,h13,h14,h15,h16,h17,h18,h19,h20,h21,h22,h23,种类) values (";
            sql = sql + "'20" + this.s2_Year + this.s2_Month + this.s2_Date + "','" + this.YB_YBH + "','" + time + "',";
            sql = sql + this.zyxs + "," + this.csxs + "," + this.cxxs + "," + this.szdz + ",'" + this.zdzs + "'," + this.szxz + ",'" + this.zxzs + "'," + this.spjz + ",";
            sql = sql + this.sz00 + "," + this.sz01 + "," + this.sz02 + "," + this.sz03 + ",";
            sql = sql + this.sz04 + "," + this.sz05 + "," + this.sz06 + "," + this.sz07 + ",";
            sql = sql + this.sz08 + "," + this.sz09 + "," + this.sz10 + "," + this.sz11 + ",";
            sql = sql + this.sz12 + "," + this.sz13 + "," + this.sz14 + "," + this.sz15 + ",";
            sql = sql + this.sz16 + "," + this.sz17 + "," + this.sz18 + "," + this.sz19 + ",";
            sql = sql + this.sz20 + "," + this.sz21 + "," + this.sz22 + "," + this.sz23 + "," + java.lang.String.valueOf(this.DataType) + ")";
        }
        else {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "update T_RSJ set 日期='20" + this.s2_Year + this.s2_Month + this.s2_Date + "',仪表号='" + this.YB_YBH + "',保存日期='" + time + "',";
            sql = sql + "总运行时=" + this.zyxs + ",超上限时=" + this.csxs + ",超下限时=" + this.cxxs + ",最大值=" + this.szdz + ",最大值时='" + this.zdzs + "',最小值=" + this.szxz + ",最小值时='" + this.zxzs + "',平均值=" + this.spjz + ",";
            sql = sql + "h00=" + this.sz00 + ",h01=" + this.sz01 + ",h02=" + this.sz02 + ",h03=" + this.sz03 + ",";
            sql = sql + "h04=" + this.sz04 + ",h05=" + this.sz05 + ",h06=" + this.sz06 + ",h07=" + this.sz07 + ",";
            sql = sql + "h08=" + this.sz08 + ",h09=" + this.sz09 + ",h10=" + this.sz10 + ",h11=" + this.sz11 + ",";
            sql = sql + "h12=" + this.sz12 + ",h13=" + this.sz13 + ",h14=" + this.sz14 + ",h15=" + this.sz15 + ",";
            sql = sql + "h16=" + this.sz16 + ",h17=" + this.sz17 + ",h18=" + this.sz18 + ",h19=" + this.sz19 + ",";
            sql = sql + "h20=" + this.sz20 + ",h21=" + this.sz21 + ",h22=" + this.sz22 + ",h23=" + this.sz23;
            sql = sql + ",种类=" + java.lang.String.valueOf(this.DataType) + " where 仪表号='" + this.YB_YBH + "' and 日期='20" + this.s2_Year + this.s2_Month + this.s2_Date + "'";
        }
        this.SQLExecute(sql);
    }
    
    public boolean checkData() {
        boolean res = true;
        this.AsynTelNo = this._Data.substring(9, 17).trim();
        if (!this.GetYBInfo()) {
            res = false;
        }
        return res;
    }
    //数据库不符
    public boolean GetYBInfo() {
        boolean res = true;
        final java.lang.String sql = "select YB.*,GS.报表小数位,yb2.行政区域码,yb2.通讯方式 from T_YB YB left outer join T_GS GS on GS.公司名称=YB.公司名称 left outer join T_YB2 yb2 on yb2.仪表号=yb.仪表号 where rtrim(通讯方式)='其他' and 唯一码='" + this.AsynTelNo + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
        try {
            rs.next();
            this.YB_YBH = rs.getString("仪表号").trim();
            this.YB_BL = rs.getFloat("倍率");
            this.sYB_BL = rs.getString("倍率");
            this.YB_GSMC = rs.getString("公司名称").trim();
            this.YB_BBXSW = rs.getInt("报表小数位");
            this.DYDJ = rs.getString("电压等级").trim();
            this.YB_BBXSW = 3;
            this.YB_floatGS = "0.";
            for (int i = 0; i < this.YB_BBXSW; ++i) {
                this.YB_floatGS += "0";
            }
            res = true;
        }
        catch (java.lang.Exception e) {
            res = false;
        }
        return res;
    }
    
    public void ReadRecvDataGetValue() throws java.lang.Exception {
        int idxStart = 0;
        if (this.DataType == 138) {
            idxStart = 20;
            this.s2_Year = java.lang.String.valueOf((int)this._Data.charAt(18));
            this.s2_Month = java.lang.String.valueOf((int)this._Data.charAt(19));
            if (this.s2_Month.length() == 1) {
                this.s2_Month = "0" + this.s2_Month;
            }
            this.s2_Date = "00";
        }
        if (this.DataType == 137) {
            idxStart = 21;
            this.s2_Year = java.lang.String.valueOf((int)this._Data.charAt(18));
            this.s2_Month = java.lang.String.valueOf((int)this._Data.charAt(19));
            if (this.s2_Month.length() == 1) {
                this.s2_Month = "0" + this.s2_Month;
            }
            this.s2_Date = java.lang.String.valueOf((int)this._Data.charAt(20));
            if (this.s2_Date.length() == 1) {
                this.s2_Date = "0" + this.s2_Date;
            }
        }
        final int offset = idxStart;
        this.zyxs = java.lang.String.valueOf(this.getValue(this._Data, offset, offset + 1));
        final java.lang.String tds = java.lang.String.valueOf(this.getValue(this._Data, offset + 2, offset + 3));
        this.csxs = java.lang.String.valueOf(this.getValue(this._Data, offset + 4, offset + 5));
        this.cxxs = java.lang.String.valueOf(this.getValue(this._Data, offset + 6, offset + 7));
        this.hgs = java.lang.String.valueOf(java.lang.Integer.valueOf(this.zyxs) - java.lang.Integer.valueOf(this.csxs) - java.lang.Integer.valueOf(this.cxxs));
        try {
            this.zdz = this.getValue(this._Data, offset + 8, offset + 9);
        }
        catch (java.lang.Exception e) {
            this.zdz = 0.0;
        }
        try {
            this.zxz = this.getValue(this._Data, offset + 12, offset + 13);
        }
        catch (java.lang.Exception e) {
            this.zxz = 9999.0;
        }
        try {
            this.pjz = this.getValue(this._Data, offset + 16, offset + 17);
        }
        catch (java.lang.Exception e) {
            this.pjz = 0.0;
        }
        if (this.DataType == 138) {
            final int maxTime = this.getValue(this._Data, offset + 10, offset + 11);
            final int minTime = this.getValue(this._Data, offset + 14, offset + 15);
            final int maxDate = maxTime / 1440;
            final int maxHour = maxTime % 1440 / 60;
            final int maxMinute = maxTime % 60;
            final int minDate = minTime / 1440;
            final int minHour = minTime % 1440 / 60;
            final int minMinute = minTime % 60;
            java.lang.String smaxDate = "";
            java.lang.String smaxHour = "";
            java.lang.String smaxMinute = "";
            java.lang.String sminDate = "";
            java.lang.String sminHour = "";
            java.lang.String sminMinute = "";
            if (maxDate < 10) {
                smaxDate = "0" + java.lang.String.valueOf(maxDate);
            }
            else {
                smaxDate = "" + java.lang.String.valueOf(maxDate);
            }
            if (maxHour < 10) {
                smaxHour = "0" + java.lang.String.valueOf(maxHour);
            }
            else {
                smaxHour = "" + java.lang.String.valueOf(maxHour);
            }
            if (maxMinute < 10) {
                smaxMinute = "0" + java.lang.String.valueOf(maxMinute);
            }
            else {
                smaxMinute = "" + java.lang.String.valueOf(maxMinute);
            }
            if (minDate < 10) {
                sminDate = "0" + java.lang.String.valueOf(minDate);
            }
            else {
                sminDate = "" + java.lang.String.valueOf(minDate);
            }
            if (minHour < 10) {
                sminHour = "0" + java.lang.String.valueOf(minHour);
            }
            else {
                sminHour = "" + java.lang.String.valueOf(minHour);
            }
            if (minMinute < 10) {
                sminMinute = "0" + java.lang.String.valueOf(minMinute);
            }
            else {
                sminMinute = "" + java.lang.String.valueOf(minMinute);
            }
            this.zdzs = this.s2_Month + smaxDate + smaxHour + smaxMinute;
            this.zxzs = this.s2_Month + sminDate + sminHour + sminMinute;
        }
        if (this.DataType == 137) {
            java.lang.String smaxHour2 = java.lang.String.valueOf((int)this._Data.charAt(offset + 10));
            if (smaxHour2.length() == 1) {
                smaxHour2 = "0" + smaxHour2;
            }
            java.lang.String smaxMinute2 = java.lang.String.valueOf((int)this._Data.charAt(offset + 11));
            if (smaxMinute2.length() == 1) {
                smaxMinute2 = "0" + smaxMinute2;
            }
            java.lang.String sminHour2 = java.lang.String.valueOf((int)this._Data.charAt(offset + 14));
            if (sminHour2.length() == 1) {
                sminHour2 = "0" + sminHour2;
            }
            java.lang.String sminMinute2 = java.lang.String.valueOf((int)this._Data.charAt(offset + 15));
            if (sminMinute2.length() == 1) {
                sminMinute2 = "0" + sminMinute2;
            }
            this.zdzs = this.s2_Month + this.s2_Date + smaxHour2 + smaxMinute2;
            this.zxzs = this.s2_Month + this.s2_Date + sminHour2 + sminMinute2;
        }
        if (this.DataType == 137) {
            this.z00 = this.getValue(this._Data, offset + 18, offset + 19);
            this.z01 = this.getValue(this._Data, offset + 20, offset + 21);
            this.z02 = this.getValue(this._Data, offset + 22, offset + 23);
            this.z03 = this.getValue(this._Data, offset + 24, offset + 25);
            this.z04 = this.getValue(this._Data, offset + 26, offset + 27);
            this.z05 = this.getValue(this._Data, offset + 28, offset + 29);
            this.z06 = this.getValue(this._Data, offset + 30, offset + 31);
            this.z07 = this.getValue(this._Data, offset + 32, offset + 33);
            this.z08 = this.getValue(this._Data, offset + 34, offset + 35);
            this.z09 = this.getValue(this._Data, offset + 36, offset + 37);
            this.z10 = this.getValue(this._Data, offset + 38, offset + 39);
            this.z11 = this.getValue(this._Data, offset + 40, offset + 41);
            this.z12 = this.getValue(this._Data, offset + 42, offset + 43);
            this.z13 = this.getValue(this._Data, offset + 44, offset + 45);
            this.z14 = this.getValue(this._Data, offset + 46, offset + 47);
            this.z15 = this.getValue(this._Data, offset + 48, offset + 49);
            this.z16 = this.getValue(this._Data, offset + 50, offset + 51);
            this.z17 = this.getValue(this._Data, offset + 52, offset + 53);
            this.z18 = this.getValue(this._Data, offset + 54, offset + 55);
            this.z19 = this.getValue(this._Data, offset + 56, offset + 57);
            this.z20 = this.getValue(this._Data, offset + 58, offset + 59);
            this.z21 = this.getValue(this._Data, offset + 60, offset + 61);
            this.z22 = this.getValue(this._Data, offset + 62, offset + 63);
            this.z23 = this.getValue(this._Data, offset + 64, offset + 65);
            if (this.DYDJ.toUpperCase().equals("35KV")) {
                this.YB_BL *= 3.5;
            }
            final java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
            this.szdz = df.format(this.zdz * this.YB_BL);
            this.szxz = df.format(this.zxz * this.YB_BL);
            this.spjz = df.format(this.pjz * this.YB_BL);
            this.sz00 = df.format(this.z00 * this.YB_BL);
            this.sz01 = df.format(this.z01 * this.YB_BL);
            this.sz02 = df.format(this.z02 * this.YB_BL);
            this.sz03 = df.format(this.z03 * this.YB_BL);
            this.sz04 = df.format(this.z04 * this.YB_BL);
            this.sz05 = df.format(this.z05 * this.YB_BL);
            this.sz06 = df.format(this.z06 * this.YB_BL);
            this.sz07 = df.format(this.z07 * this.YB_BL);
            this.sz08 = df.format(this.z08 * this.YB_BL);
            this.sz09 = df.format(this.z09 * this.YB_BL);
            this.sz10 = df.format(this.z10 * this.YB_BL);
            this.sz11 = df.format(this.z11 * this.YB_BL);
            this.sz12 = df.format(this.z12 * this.YB_BL);
            this.sz13 = df.format(this.z13 * this.YB_BL);
            this.sz14 = df.format(this.z14 * this.YB_BL);
            this.sz15 = df.format(this.z15 * this.YB_BL);
            this.sz16 = df.format(this.z16 * this.YB_BL);
            this.sz17 = df.format(this.z17 * this.YB_BL);
            this.sz18 = df.format(this.z18 * this.YB_BL);
            this.sz19 = df.format(this.z19 * this.YB_BL);
            this.sz20 = df.format(this.z20 * this.YB_BL);
            this.sz21 = df.format(this.z21 * this.YB_BL);
            this.sz22 = df.format(this.z22 * this.YB_BL);
            this.sz23 = df.format(this.z23 * this.YB_BL);
        }
    }
    
    private int getValue(final java.lang.String _data, final int startIndex, final int endIndex) {
        int res = 0;
        final int lo = _data.charAt(startIndex);
        final int hi = _data.charAt(endIndex);
        res = hi * 255 + lo;
        return res;
    }
    
    public void SQLExecute(final java.lang.String sql) {
        synchronized (javagprssrv.JGS_Param.UnSavedDBSQLString) {
            javagprssrv.JGS_Param.UnSavedDBSQLString.add(sql);
        }
    }
}
